//
//  GJVerticalButton.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "GJVerticalButton.h"
#import "UIView+WZLBadge.h"

@interface GJVerticalButton ()

@property (nonatomic, strong) UIImageView *gImageView;

@property (nonatomic, strong) UILabel *gLabel;

@end

@implementation GJVerticalButton

+ (instancetype)buttonWithType:(UIButtonType)buttonType
{
    GJVerticalButton *btn = [super buttonWithType:buttonType];
    [btn setBackgroundImage:imageWithColor([UIColor whiteColor]) forState:UIControlStateNormal];
    [btn setBackgroundImage:imageWithColor(UIColorFromRGB(0xf5f5f5)) forState:UIControlStateHighlighted];
    return btn;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self setBackgroundImage:imageWithColor([UIColor whiteColor]) forState:UIControlStateNormal];
        [self setBackgroundImage:imageWithColor(UIColorFromRGB(0xf5f5f5)) forState:UIControlStateHighlighted];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}

- (void)setMargin:(CGFloat)margin
{
    _margin = margin;
    self.height = self.imageWidth+margin+13;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    self.gImageView.image = image;
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    self.gLabel.text = title;
}

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
{
    self.gLabel.textColor = color;
}

- (UIImageView *)gImageView
{
    if (_gImageView) {
        return _gImageView;
    }
    if (self.imageWidth>self.width) {
        self.imageWidth = self.width;
    }
    _gImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width-self.imageWidth)/2.f, (self.height-self.imageWidth-self.margin-13)/2.f, self.imageWidth, self.imageWidth)];
    _gImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
    [self addSubview:_gImageView];
    return _gImageView;
}

- (UILabel *)gLabel
{
    if (_gLabel) {
        return _gLabel;
    }
    _gLabel = [UILabel labelWithFrame:CGRectMake(0, CGRectGetMaxY(self.gImageView.frame)+self.margin, self.width, 13) text:nil fontSize:13 textColor:UIColorFromRGB(0x666666) textAlignment:NSTextAlignmentCenter];
    _gLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self addSubview:_gLabel];
    return _gLabel;
}

- (void)setBadgeValue:(NSInteger)number
{
    self.badgeCenterOffset = CGPointMake(-(self.width-(self.imageWidth+self.margin+13))/2.f, (self.height-(self.imageWidth+self.margin+13))/2.f);
    [self showBadgeWithStyle:WBadgeStyleNumber value:number];
    self.badge.text=nil;
}

@end
